package cn.tp.ksoft.civil_driver.module.mime;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import cn.tp.ksoft.civil_driver.DriverBaseActivity;
import cn.tp.ksoft.civil_driver.R;
import cn.tp.ksoft.civil_driver.bean.DriverDeviceDto;
import cn.tp.ksoft.civil_driver.bean.ResultBean;
import cn.tp.ksoft.civil_driver.request.DriverRequest;
import cn.tp.ksoft.civil_driver.utils.DeviceUtils;
import cn.tp.ksoft.civil_driver.utils.TimestampTypeAdapter;
import cn.tp.ksoft.civil_driver.view.simple.SpaceItemDecoration;
import cn.tp.ksoft.library_civil.utils.StringUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

public class ModifyDeviceActivity extends DriverBaseActivity {
    private final int ITEM_SPACE = 10;
	private RecyclerView mRv;
    private LinearLayoutManager mLayoutManager;
	private DeviceAdapter mAdapter;
	private TextView tvUsingDevice;
	private List<DriverDeviceDto> list;
	private DriverDeviceDto bean;
	private DriverRequest request;
	Gson gson;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.driver_activity_modify_device);
		mRv= (RecyclerView) findViewById(R.id.lv_devices);
		tvUsingDevice = (TextView) findViewById(R.id.tv_device_using);
        mAdapter = new DeviceAdapter();
        mLayoutManager = new LinearLayoutManager(this);
        mRv.setAdapter(mAdapter);
        mRv.setLayoutManager(mLayoutManager);
        mRv.setItemAnimator(new DefaultItemAnimator());
        DisplayMetrics dm = getResources().getDisplayMetrics();
        mRv.addItemDecoration(new SpaceItemDecoration(Math.round(ITEM_SPACE * dm.density)));
        updateTitle("设备管理");
		bean = DeviceUtils.getDeviceInfo(this);
		tvUsingDevice.setText(bean.getOsInfo());
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder.registerTypeAdapter(Date.class, new TimestampTypeAdapter());
		gson = gsonBuilder.create();
		request = new DriverRequest(this, handler);
		request.getDeviceList();
		
	}
	Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			ResultBean res = (ResultBean) msg.obj;
			if (res.isSuccess()) {
				String data = gson.toJson(res.getData());
				list = gson.fromJson(data,
						new TypeToken<List<DriverDeviceDto>>() {}.getType());
				if(!StringUtils.isEmpty(list)){
					mAdapter.notifyDataSetChanged();
				}
			}
		}
	};
    class DeviceHolder extends RecyclerView.ViewHolder {
        TextView mTvName;
        TextView mTvDate;
        public DeviceHolder(View itemView) {
            super(itemView);
            mTvName = (TextView) itemView.findViewById(R.id.tv_item_device_name);
            mTvDate = (TextView) itemView.findViewById(R.id.tv_item_device_date);
        }
    }
    class DeviceAdapter extends RecyclerView.Adapter<DeviceHolder> {
        @Override
        public DeviceHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.driver_my_device_item, viewGroup, false);
            return new DeviceHolder(view);
        }

        @Override
        public void onBindViewHolder(DeviceHolder holder, int position) {
            final DriverDeviceDto vo = list.get(position);
            holder.mTvName.setText(vo.getOsInfo());
    		if (vo.getTime() != null) {
    			holder.mTvDate.setText((new java.text.SimpleDateFormat("首次使用："+"yyyy-MM-dd"))
    							.format(vo.getTime()));
    		}
        }

        @Override
        public int getItemCount() {
            return null != list ? list.size() : 0;
        }
    }
}
